﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using S4Store.Service;
using S4Store.Domain;
using LX.Web.Mvc;
using S4Store.Models.Store;

namespace S4Store.Controllers
{
    public class StoreController : BaseController
    {

        public StoreService StoreService { get; set; }
        public CarService CarService { get; set; }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Query()
        {
            QueryModel query = new QueryModel
            {
                Page = 1,
                PageSize = 20
            };
            return Query(query);
        }

        [HttpPost]
        public ActionResult Query(QueryModel query)
        {
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                var querysource = StoreService.GetQuery();
                if (!Helper.StringIsEmpty(query.Name))
                {
                    querysource = querysource.Where(x => x.Name.Contains(query.Name));
                }
                if (!Helper.StringIsEmpty(query.Contact))
                {
                    querysource = querysource.Where(x => x.Contact.Contains(query.Contact));
                }
                if (!Helper.StringIsEmpty(query.Tel))
                {
                    querysource = querysource.Where(x => x.Tel.Contains(query.Tel));
                }
                if (!Helper.StringIsEmpty(query.Address))
                {
                    querysource = querysource.Where(x => x.Address.Contains(query.Address));
                }
                if (!Helper.StringIsEmpty(query.Code))
                {
                    querysource = querysource.Where(x => x.Code.Contains(query.Code));
                }
                if (query.Pinpai!=null)
                {
                    querysource = querysource.Where(x => x.PinPaiId==query.Pinpai.Value);
                }
                int total = querysource.Count();
                querysource = querysource.OrderByDescending(x => x.Id);
                if (query.Page > 1 && query.PageSize > 0)
                {
                    querysource = querysource.Skip((query.Page - 1) * query.PageSize);
                }
                querysource = querysource.Take(query.PageSize);
                LX.Core.Query.PagedList<Store> data = new LX.Core.Query.PagedList<Store>
                {
                    Data = querysource.ToList(),
                    Page = query.Page,
                    PageSize = query.PageSize,
                    Total = total

                };
                t.Commit();
                return View(data);
            } 
        }

        public ActionResult Add()
        {
            List<S4Store.Domain.PinPai> pinpais = new List<PinPai>();
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                pinpais = CarService.GetEnablePinPai().ToList();
                t.Commit();

                return View(pinpais);
            } 
        }

        [HttpPost]
        public JsonResult Add(Store store)
        {
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                StoreService.AddS4Store(store);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success=true
                    }
                };
            } 
        }

        [HttpPost]
        public ActionResult Modify(int id)
        {
            Combin<Store, List<S4Store.Domain.PinPai>> model = new Combin<Store, List<PinPai>>();
            using (var t = new Transtion())
            {
                t.Inject<CarService>(CarService);
                t.Inject<StoreService>(StoreService);
                model.Data1 = StoreService.GetQuery().FirstOrDefault(x => x.Id == id);
                model.Data2 = CarService.GetEnablePinPai().ToList();
                t.Commit();
                if (model != null)
                {
                    return View(model);
                }
                return this.Content("店不存在");
            }  

            
        }


        [HttpPost]
        public JsonResult ModifyBack(Store store)
        {
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                StoreService.ModifyS4Store(store);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

        [HttpPost]
        public JsonResult Delete(List<int> ids)
        {
            if(Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要删除的项");
            }
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                foreach (var id in ids)
                {
                    StoreService.DeleteS4Store(id);
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "删除成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    
    }
}
